<template>
  <div class="flex gap-4">
    <div>
      <el-autocomplete
          v-model="queryString"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          clearable
          class="inline-input w-50"
          placeholder="Please Input"
          @select="handleSelect"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { mockMenu } from "../../../mock/menu";
import router from "../../router";

const queryString = ref('');

interface RestaurantItem {
  value: string
  link: string
}

//搜索菜单
const restaurants = ref<RestaurantItem[]>([])
const querySearch = (queryString: string, cb: any) => {
  const results = queryString
      ? restaurants.value.filter(createFilter(queryString))
      : restaurants.value
  cb(results)
}

//搜索过滤器
const createFilter = (queryString: string) => {
  return (restaurant: RestaurantItem) => {
    return (
        restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
    )
  }
}

//选中菜单
const handleSelect = (item) => {
  console.log("选中菜单：" + JSON.stringify(item))
  router.push(item.link);
}

//初始化菜单数据
onMounted(() => {
  mockMenu.forEach(item => {
    restaurants.value.push({value: item.title, link: item.index})
  })
})
</script>
